
var frm;
Ext.onReady(function(){
	    //初始化标签中的Ext:Qtip属性。
        Ext.QuickTips.init();
        Ext.form.Field.prototype.msgTarget = 'side';
        tableds.tabBar.items.items[1].hide();
        win.show();
      //  grids.show();
	});


//表头按钮
var toolbar=Ext.create('Ext.toolbar.Toolbar',{
	renderTo:document.body,
	width:550,
	items:[
	       {text:'上一页',handler:function(){dwhshangyiye(this.text)}},
	       {text:'下一页',handler:function(){dwhshangyiye(this.text)}},
	       {text:'新建',handler:function(){fromqingkong();}},
	       {text:'保存',handler:function(){add();}},
	       {text:'修改',handler:function(){xiugai();}},
	       {text:'删除',handler:function(){dwhDelete();}},
	       {text:'刷新',handler:function(){dwhxianshi();}},
	       {xtype:'tbspacer',width:150},
	       {text:'审核',handler:function(){dwhshenhe();}},
	       {text:'反审核',handler:function(){dwhfanshenhe()}}
	       ],
});


var datajsons;
jiazai(0);
function jiazai(th){
	Ext.Ajax.request(   
		    {  
		        //被用来向服务器发起请求默认的url  
		        url : "/chaxun",  
		        params:{page:th},
		        //请求时使用的默认的http方法  
		        method : "post",  
		        //请求成功时回调函数  
		        success : function(data) {  
		        	var temp=JSON.parse(data.responseText);
		        	datajsons=temp;
		        	//alert(datajsons.pageobj[0].zhuangtai);
		        	grids.getStore().removeAll();
					var temp1=datajsons.pageobj[0].prepaymentmaindetailPojos;
					Ext.getCmp('fundbillid').setValue(datajsons.pageobj[0].fundbillid);
					Ext.getCmp('customerid').setValue(datajsons.pageobj[0].customerid);
					Ext.getCmp('fullname').setValue(datajsons.pageobj[0].fullname);
					Ext.getCmp('fundbilldate').setValue(datajsons.pageobj[0].fundbilldate);
					Ext.getCmp('cashstylename').setValue(datajsons.pageobj[0].cashstylename);
					Ext.getCmp('cash').setValue(datajsons.pageobj[0].cash);
					Ext.getCmp('fundbillid').setValue(datajsons.pageobj[0].fundbillid);
					Ext.getCmp('visastylename').setValue(datajsons.pageobj[0].visastylename);
					Ext.getCmp('visa').setValue(datajsons.pageobj[0].visa);
					Ext.getCmp('currencyname').setValue(datajsons.pageobj[0].currencyname);
					Ext.getCmp('otherpaystylename').setValue(datajsons.pageobj[0].otherpaystylename);
					Ext.getCmp('otherpay').setValue(datajsons.pageobj[0].otherpay);
					Ext.getCmp('exchrate').setValue(datajsons.pageobj[0].exchrate);
					
					Ext.getCmp('discountper').setValue(datajsons.pageobj[0].discountper);
					Ext.getCmp('offsetstyle').setValue(datajsons.pageobj[0].offsetstyle);
					Ext.getCmp('accmonthromonth').setValue(datajsons.pageobj[0].accmonthromonth);
					Ext.getCmp('pingzhengbianhao').setValue(datajsons.pageobj[0].pingzhengbianhao);
					//尾部
					Ext.getCmp('deptfrom').setValue(datajsons.pageobj[0].deptfrom);
					Ext.getCmp('maker').setValue(datajsons.pageobj[0].maker);
					Ext.getCmp('projectfrom').setValue(datajsons.pageobj[0].projectfrom);
					Ext.getCmp('permitter').setValue(datajsons.pageobj[0].permitter);
					Ext.getCmp('zhuangtai').setValue(datajsons.pageobj[0].zhuangtai);
					grids.getStore().add(temp1);
//		             alert(response.responseText);
//		             alert(JSON.stringify(e));
		        	Ext.Msg.alert("提示","加载成功!"); 
		        },  
		        //请求失败时回调函数  
		        failure : function() {  
		            Ext.ux.Toast.msg("信息提示", "信息删除出错，请联系管理员!");  
		        }  
		    }  
		);  

}


 

	var fielmenu2=new Ext.menu.Menu({
		items:[{text:'载入单币别账款'},{text:'载入单币别账款'}]
	});
	var fielmenu3=new Ext.menu.Menu({
		items:[{text:'以下冲款'},{text:'以下折让'}]
	});
	
	
	//数据表格
    var grids=Ext.create('Ext.grid.Panel',{ 
    	 listeners:{
				containerdblclick: function(grid, e, eOpts){ //单击事件
	    						grid.getStore().add({'rownumberer':'', 'addMoney':'','billType':'','orderNO':'','description':''});
 					},
			},plugins: [
     				Ext.create('Ext.grid.plugin.CellEditing', {
         				clicksToEdit: 1
     				})
 				],
    	columns: [   {header: '（栏号）',	xtype: 'rownumberer',	dataIndex: 'lh', width: 50,sortable: false},          					
           	 	     { header: '预付金额',  dataIndex: 'addMoney', width: 100,editor:"textfield"},                 	    
          			 { header: '来源别', dataIndex: 'billType' ,editor:"textfield"},
   					 { header: '来源单号', dataIndex: 'orderNO' ,editor:"textfield"},
   					 { header: '摘要', dataIndex: 'description',editor:"textfield"}
      			 ],
      	 height: 120,
   		 width:671,
   		 autoScroll:false
   })
   
   //数据表格
    var gridsyufu=Ext.create('Ext.grid.Panel',{ 
    	 listeners:{
				containerdblclick: function(grid, e, eOpts){ //单击事件
	    						grid.getStore().add({'rownumberer':'', 'addMoney':'','billType':'','orderNO':'','description':''});
 					},
			},plugins: [
     				Ext.create('Ext.grid.plugin.CellEditing', {
         				clicksToEdit: 1
     				})
 				],
    	columns: [   {header: '（应付冲款单号）',	xtype: 'rownumberer',	dataIndex: 'lh', width: 50,sortable: false},          					
           	 	     { header: '(来源别)',  dataIndex: 'addMoney', width: 100},                 	    
          			 { header: '(来源单号)', dataIndex: 'billType' },
   					 { header: '(预付余额)', dataIndex: 'orderNO'},
   					 { header: '(部门编号)', dataIndex: 'description'},
   					 { header: '(部门名称)', dataIndex: 'description'},
   					 { header: '取用预付金额', dataIndex: 'description',editor:"textfield",},
   					 { header: '(来源摘要)', dataIndex: 'description'}
      			 ],
      	 height: 120,
   		 width:671,
   		 autoScroll:false
   })

   var filterPanel = Ext.create('Ext.panel.Panel', {
       bodyPadding: 6,  // 避免Panel中的子元素紧邻边框
       width: 1985,
       height:160,
       items:[grids],
   	bbar:[{xtype:"label",text:'总计:',width:90},
   	{xtype:"textfield",disabled:true,width:90,margin:'0 180',value:'0.00'},
   	{xtype:"textfield",disabled:true,width:90,margin:'0',value:'0.00'},
   	{xtype:"textfield",disabled:true,width:90,margin:'0 40 0 30',value:'0.00'},
   	{xtype:"textfield",disabled:true,width:90,margin:'0 0 0 0',value:'0.00'}],
   });
    
    var filterPanelyufu = Ext.create('Ext.panel.Panel', {
        bodyPadding: 6,  // 避免Panel中的子元素紧邻边框
        width: 1985,
        height:160,
        items:[gridsyufu],
    	bbar:[{xtype:"label",text:'总计:',width:90},
    	{xtype:"textfield",disabled:true,width:90,margin:'0 180',value:'0.00'},
    	{xtype:"textfield",disabled:true,width:90,margin:'0',value:'0.00'},
    	{xtype:"textfield",disabled:true,width:90,margin:'0 40 0 30',value:'0.00'},
    	{xtype:"textfield",disabled:true,width:90,margin:'0 0 0 0',value:'0.00'}],
    });

	
//选项卡
var  tableds=Ext.create('Ext.TabPanel',{
	width:"100%",
	height:230,
	autoScroll:true,
	bodyPadding: 5,
	items:[{layout: 'fit', index: 0,title:'内容',autoScroll:true,items:[filterPanel]},
	       {layout: 'fit', index: 1,title:'取用预付',autoScroll:true,items:[filterPanelyufu]},
	       {layout: 'fit', index: 2,title:'备注',xtype:'textarea',id:'beizhu',name:'Remark',fieldLabel: '备注',labelWidth: 30}],
});

var formsg=Ext.create('Ext.form.Panel', {
    bodyPadding: 5,
	height:200,
	width:590,
	layout:"column",
	baseCls:'x-plain',
	items:[{
	    id:"deptfrom",
		xtype: 'textfield',
        name: 'deptfrom',
		bodyPadding: 5,
        fieldLabel: '所属部门',
		//width:200,
        labelWidth: 80,
        //anchor: '100%',
	},{
		id:"maker",
		xtype: 'textfield',
        name: 'maker',
        fieldLabel: '制单人员',
		style:'margin-left:19px',
		//width:200,
        labelWidth: 80,
        //anchor: '100%',
	},{
	    id:"projectfrom",
		xtype: 'textfield',
        name: 'projectfrom',
        fieldLabel: '所属项目',
		//width:200,
        labelWidth: 80,
       // anchor: '100%',
	},{
		id:"permitter",
		xtype: 'textfield',
        name: 'Permitter',
        fieldLabel: '复核人员',
		style:'margin-left:19px',
		//width:200,
        labelWidth: 80,
        //anchor: '100%',
    	}],
	});

//表单
var form = new Ext.form.FormPanel({
	    frame: true,//背景颜色
	    margin: 0,
	    renderTo: Ext.getBody(),
	    autoScroll: true,   //自动创建滚动条
	    id : "frm", 
	    layout:"column",
	    items: [{
	    	id:"customerid",
    	    xtype: "textfield",
	    	height:18, 
	    	name: "customerid", 
	    	hidden:true,
	    	hideLabel:true,
	    	allowBlank: true 
    	    },{
	    	id:"fullname",
    	    xtype: "textfield",
	    	height:18, 
	    	name: "fullname", 
	    	labelStyle:"width:80px;line-height:10px;",
	    	fieldLabel: "供应商",
	    	allowBlank: true 
    	    },{
		    	id:"zhuangtai",
	    	    xtype: "textfield",
		    	name: "zhuangtai", 
		    	hidden:true,
		    	hideLabel:true ,
		    	allowBlank: true 
	    	},{
		    	id:"fundbilldate",
	    		xtype: "datefield",
		    	style:"height:18px;", 
		    	name: "fundbillDate", 
		    	labelStyle:"width:80px;line-height:10px;",
		    	fieldLabel: "单据日期",
		    	format: "Y-m-d",//日期的格式
			    altFormats: "Y/m/d|Ymd",
		    	allowBlank: true 
		    },{
		    	id:"cashstylename",
		    	xtype: "textfield",
		    	height:18, 
		    	width:180,
		    	name: "cashstylename", 
		    	labelWidth:80,
		    	fieldLabel: "结算方式一",
		    	value:"转账",
		    	allowBlank: true 
		    },{
		    	id:"cash",
		    	xtype: "textfield",
		    	height:18, 
		    	name: "cash", 
		    	width:55,
		    	allowBlank: true 
		    },{
		    	id:"fundbillid",
		    	xtype: "textfield",
		    	height:18, 
		    	name: "fundbillid", 
		    	labelStyle:"width:80px;line-height:10px;",
		    	fieldLabel: "单据号码",
		    	style:"margin-left:20px;",
		    	allowBlank: true 
	    	},{
		    	id:"visastylename",
	    		xtype: "textfield",
		    	height:18, 
		    	width:180,
		    	name: "visastylename", 
		    	labelWidth:80,
		    	value:"汇票",
		    	fieldLabel: "结算方式二",
		    	allowBlank: true 
		    },{
		    	id:"visa",
		    	xtype: "textfield",
		    	height:18, 
		    	name: "visa", 
		    	width:55,
		    	allowBlank: true 
		    },{
		    	id:"currencyname",
		    	xtype: "textfield",
		    	height:18, 
		    	name: "currencyname", 
		    	labelStyle:"width:80px;line-height:10px;",
		    	fieldLabel: "币别",
		    	style:"margin-left:20px;",
		    	allowBlank: true 
	    	},{
		    	id:"otherpaystylename",
		    	xtype: "textfield",
		    	height:18, 
		    	width:180,
		    	name: "otherpaystylename", 
		    	labelWidth:80,
		    	fieldLabel: "结算方式三",
		    	allowBlank: true 
		    },{
		    	id:"otherpay",
		    	xtype: "textfield",
		    	height:18, 
		    	name: "otherpay", 
		    	width:55,
		    	allowBlank: true 
		    },{
		    	id:"exchrate",
		    	xtype: "textfield",
		    	height:18, 
		    	name: "exchrate", 
		    	labelStyle:"width:80px;line-height:10px;",
		    	fieldLabel: "汇率",
		    	style:"margin-left:20px;",
		    	allowBlank: true 
	    	},{
		    	id:"discountper",
	    		xtype: "textfield",
		    	height:18, 
		    	name: "discountper", 
		    	labelStyle:"width:80px;line-height:10px;",
		    	fieldLabel: "折扣率",
		    	allowBlank: true 
	    	},{
		    	id:"offsetstyle",
	    		xtype: "combo",
		    	height:18, 
		    	name: "offsetstyle", 
		    	labelStyle:"width:80px;line-height:10px;",
		    	fieldLabel: "应付冲款类型",
		    	allowBlank: true ,
		    	valueField:'a',
		    	displayField:'s',
		    	store:Ext.create('Ext.data.Store',{
		    		fields:['a','s'],
		    		data:[
		    		  {'a':'预付款','s':'预付款'},
		    		  {'a':'预付冲应付','s':'预付冲应付'}
		    		  ]
		    	}),
		    	value:'预付款',
		    	listeners : {
		    		"select" : function(){
		    			 var url = this.getValue();//得到valueField的值 
		    			 if(url=="预付款"){
		    				 tableds.tabBar.items.items[1].hide();
		    			 }
		    			 if(url=="预付冲应付"){
		    				 tableds.tabBar.items.items[1].show();
		    			 }
		    		}
		    	}
		    	
	    	},{
		    	id:"accmonthromonth",
	    		xtype: "datefield",
		    	style:"height:18px;", 
		    	name: "accmonthromonth", 
		    	labelStyle:"width:80px;line-height:10px;",
		    	fieldLabel: "终止帐月",
		    	format: "Y-m-d",//日期的格式
			    altFormats: "Y/m/d|Ymd",
		    	allowBlank: true 
	    	},{
		    	id:"pingzhengbianhao",
	    		xtype: "textfield",
		    	height:18, 
		    	name: "pingzhengbianhao", 
		    	labelStyle:"width:80px;line-height:10px;",
		    	fieldLabel: "凭证编号",
		    	disabled:true,
		    	allowBlank: true 
	    	},tableds,formsg]
    
});
 

	/**
	 * 窗体
	 */
	var win = new Ext.Window({
	    title: '窗口',
	    width: 556,
	    height: 514,
	    html: '',
	    resizable: true,
	    modal: true,
	    closable: true,
	    maximizable: true,
	    minimizable: true,
	    items: [toolbar,form],
	    bbar:[
	  		{xtype:"splitbutton",text:'功能',width:90,menu:fielmenu2},
	  		{xtype:"splitbutton",text:'自动计算',width:90,menu:fielmenu3}]
	});

	
	var zhubiaocongbiao;
	/**
	 * 添加主表和从表
	 * @returns
	 */
	function add(){
		 zhubiaocongbiao = {
				fullname:Ext.getCmp('fullname').getValue(),
				fundbilldate:Ext.getCmp('fundbilldate').getValue(),
				cashstylename:Ext.getCmp('cashstylename').getValue(),
				cash:Ext.getCmp('cash').getValue(),
				fundbillid:Ext.getCmp('fundbillid').getValue(),
				visastylename:Ext.getCmp('visastylename').getValue(),
				visa:Ext.getCmp('visa').getValue(),
				currencyname:Ext.getCmp('currencyname').getValue(),
				otherpaystylename:Ext.getCmp('otherpaystylename').getValue(),
				otherpay:Ext.getCmp('otherpay').getValue(),
				exchrate:Ext.getCmp('exchrate').getValue(),
				
				discountper:Ext.getCmp('discountper').getValue(),
				offsetstyle:Ext.getCmp('offsetstyle').getValue(),
				accmonthromonth:Ext.getCmp('accmonthromonth').getValue(),
				pingzhengbianhao:Ext.getCmp('pingzhengbianhao').getValue(),
				//尾部
				deptfrom:Ext.getCmp('deptfrom').getValue(),
				maker:Ext.getCmp('maker').getValue(),
				projectfrom:Ext.getCmp('projectfrom').getValue(),
				permitter:Ext.getCmp('permitter').getValue(),
				shanchuzhuangtai:0,
				zhuangtai:"未审核",
				prepaymentmaindetailPojos:Ext.pluck(grids.getStore().data.items,'data'),
		}
		 
		 
		 

		//alert(JSON.stringify(zhubiaocongbiao));
		Ext.Ajax.request({
			url:'/insertdwh',
		    method : "post",  
			headers:{"Content-Type":"application/json"},
			params:JSON.stringify(zhubiaocongbiao),
			success:function(response){
				Ext.MessageBox.alert("提示","新增成功",function(){});
				grids.getStore().removeAll();
				jiazai(0);
			}
		});
	}
	
	
	/**
	 * from表单清空
	 * @returns
	 */
	function fromqingkong(){
		//清空数据
		Ext.getCmp('frm').form.reset();
		grids.getStore().removeAll();
		zidongdanhao();//自动单号
	}
	
	
	
	/**
	 * 上一页和下一页
	 * @param th
	 * @returns
	 */
	function dwhshangyiye(th){
		if(th=='下一页'){
			if(datajsons.nextPage>datajsons.pagesize-1){
				Ext.Msg.alert("提示","您好！已经没有下一页哦");
				return;
			}
			jiazai(datajsons.nextPage);
			grids.getStore().removeAll();
		}else{
			if(datajsons.topPage>datajsons.currentPage){
				Ext.Msg.alert("提示","您好！已经没有上一页了哦");
				return;
			}
			jiazai(datajsons.topPage);
			grids.getStore().removeAll();
		}
	}
	
	/**
	 * 自动单号
	 * @returns
	 */
	function zidongdanhao(){
		Ext.Ajax.request({
			url:"dwhzidongdanhao",
			params:{fundBilldate:Ext.getCmp('fundbilldate').getValue()},
			method:'post',
			success:function(e,response){
				Ext.getCmp('fundbillid').setValue(e.responseText);
				//Ext.util.Format.date(Ext.Date.add(new Date(),Ext.Date.MONTH,-1),"Y-m-d") 
				Ext.getCmp('fundbilldate').setValue(Ext.Date.add(new Date()),"Y-m-d");//设置日期
				
			}
		});
	}
	
	
	/**
	 * 审核
	 * @returns
	 */
	function dwhshenhe(){
		var fundbillid =Ext.getCmp('fundbillid').getValue();
		var zhuangtai = Ext.getCmp('zhuangtai').getValue();
		var gongyingshang = Ext.getCmp('customerid').getValue();
		
		 if(zhuangtai == "未审核"){
			 var rowCount=grids.getStore().getCount();
			 alert(rowCount);
			 var data;
	        	for (var i=0;i<rowCount;i++){
	        	  data = grids.getStore().getAt(i).data;
	        	}
	        	 alert(data.addMoney);
			 Ext.Ajax.request({  
					        //被用来向服务器发起请求默认的url  
					        url : "/dwhshenhe",  
					        //请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串  
					        params:{fundBillID:fundbillid,money:data.addMoney,gongyingshangID:gongyingshang},
					        //请求时使用的默认的http方法  
					        method : "post",  
					        //请求成功时回调函数  
					        success : function(e, response) {  
					        	Ext.Msg.alert("提示","审核成功!"); 
					        	jiazai(0);
					        	
					        }
					       
		   }); 
		 }else{
			 Ext.Msg.alert("提示","此单已审核，不能在处理！！！!");
			
		 }
	}
	
	
	/**
	 * 反审核
	 * @returns
	 */
	function dwhfanshenhe(){
		var fundbillid =Ext.getCmp('fundbillid').getValue();
		var zhuangtai = Ext.getCmp('zhuangtai').getValue();
		var gongyingshang = Ext.getCmp('customerid').getValue();
		
		 if(zhuangtai == "已审核"){
			 var rowCount=grids.getStore().getCount();
			 alert(rowCount);
			 var data;
	        	for (var i=0;i<rowCount;i++){
	        	  data = grids.getStore().getAt(i).data;
	        	}
	        	 alert(data.addMoney);
			 Ext.Ajax.request({  
					        //被用来向服务器发起请求默认的url  
					        url : "/dwhshenheFan",  
					        //请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串  
					        params:{fundBillID:fundbillid,money:data.addMoney,gongyingshangID:gongyingshang},
					        //请求时使用的默认的http方法  
					        method : "post",  
					        //请求成功时回调函数  
					        success : function(e, response) {  
					        	Ext.Msg.alert("提示","审核成功!"); 
					        	jiazai(0);
					        }
					       
		   }); 
		 }else{
			 Ext.Msg.alert("提示","此单未审核，不能反审核！！！!");
		 }
	}
	
	/**
	 * 删除，逻辑删除
	 * @returns
	 */
	function dwhDelete(){
		var funid =Ext.getCmp('fundbillid').getValue();
		Ext.Msg.alert("提示",funid);
		alert(funid);
		Ext.Ajax.request(   
			    {  
			        //被用来向服务器发起请求默认的url  
			        url : "/dwhdelete",  
			        //请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串  
			        params:{fundBillID:Ext.getCmp('fundbillid').getValue()},
			        //请求时使用的默认的http方法  
			        method : "post",  
			        //请求成功时回调函数  
			        success : function(e, response) {  
			        	Ext.Msg.alert("提示","删除成功!"); 
			        	grids.getStore().removeAll();
			        	jiazai(0);
			        	
			        }
			       
			        }   
			);  
	}
	
	/**
	 * 修改
	 * @returns
	 */
	function xiugai(){
		var data1 = form.getForm().getValues();
		var data2 = formsg.getForm().getValues();
		var data3 = Ext.pluck(grids.getStore().data.items,"data");
		alert(JSON.stringify(data3))
		Ext.each(data3,function(item){
			item.fundbillid = data1.fundbillid;
		});
		var datas = Ext.apply(data1,data2);
		datas.prepaymentmaindetailPojos = data3;
		alert(JSON.stringify(datas));
		Ext.Ajax.request({  
			        //被用来向服务器发起请求默认的url  
			        url : "/dwhdeletezhubiaocongbiao",  
			        //请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串  
			        method : "post",  
			    	headers:{'Content-Type':"application/json"},
			    	 params:JSON.stringify(datas),
			        //请求时使用的默认的http方法  
			      
			        //请求成功时回调函数  
			        success : function(e, response) {  
			        	Ext.Msg.alert("提示","修改成功!"); 
			        	grids.getStore().removeAll();
			        	jiazai(0);
			        }
	   });  
	}
	
	/**
	 * 隐藏显示
	 * @returns
	 */
	function dwhxianshi(){
		var off = Ext.getCmp('offsetstyle').getValue();
		alert(Ext.getCmp('offsetstyle').getValue());
		
		if(off=="预付款"){
			tableds.tabBar.items.items[1].hide();
		}
		if(off=="预付冲应付"){
			tableds.tabBar.items.items[1].show();
		}
		
		
	}
	/**load**/  
	function load() {   
	 frm.getForm().load({   
	  waitMsg : "正在加载数据……",   
	  waitTitle : "提示",   
	  url : "/chaxun",   
	  method : "post",   
	  success : function (form, action) {   
		  alert(JSON.stringify(form))
	   Ext.Msg.alert("提示","加载成功!");   
	  }   
	 })   
	}   